// models/word.js
const connection = require("../database"); // 假设这是您的数据库连接模块

const Word = {
	create: (wordData, callback) => {
		const query = `INSERT INTO words (name, description, samewordlist) VALUES (?, ?, ?)`;
		connection.query(
			query,
			[
				wordData.name,
				wordData.description,
				JSON.stringify(wordData.samewordlist || []),
			],
			callback
		);
	},
	findOne: (wordId, callback) => {
		const query = `SELECT * FROM words WHERE id = ?`;
		connection.query(query, [wordId], callback);
	},
	update: (wordId, wordData, callback) => {
		const query = `UPDATE words SET name = ?, description = ?, samewordlist = ? WHERE id = ?`;
		connection.query(
			query,
			[
				wordData.name,
				wordData.description,
				JSON.stringify(wordData.samewordlist),
				wordId,
			],
			callback
		);
	},
	delete: (wordId, callback) => {
		const query = `DELETE FROM words WHERE id = ?`;
		connection.query(query, [wordId], callback);
	},
	findOneByName: (name, callback) => {
		const query = `SELECT * FROM words WHERE name LIKE ?`;
		connection.query(query, [`%${name}%`], callback);
	},
	findOneName: (name, callback) => {
		const query = `SELECT * FROM words WHERE name = ?`;
		connection.query(query, [name], callback);
	},
	findAll: (page, limit, callback) => {
		// page是当前页码，limit是每页显示的数量
		// 计算OFFSET值
		const offset = (page - 1) * limit;
		const query = `
      SELECT * FROM words
      LIMIT ?
      OFFSET ?
    `;
		connection.query(query, [parseInt(limit), parseInt(offset)], callback);
	},
};

module.exports = Word;
